package com.bana.fvs.app.module.tab2.tab2Detail.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.module.tab2.tab2Detail.entity.SingleDetailItemBean;

import java.util.List;

/**
 * Author : yanftch
 * Date : 2018/2/27
 * Time : 16:37
 * Desc :
 */

/**
 * type==0------>能见度检测仪
 * type==1------>区域控制器
 * type==2------>雾灯
 */
public class SingleAllAdapter extends RecyclerView.Adapter<SingleAllAdapter.ViewHolder> {
    private static final String TAG = "dah_SingleAllAdapter";

    private List<SingleDetailItemBean> datas;
    private int type;

    public SingleAllAdapter(List<SingleDetailItemBean> datas, int type) {
        this.type = type;
        this.datas = datas;
        Log.e(TAG, "SingleAllAdapter: type===" + type);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_single_all_view, parent, false);
        return new SingleAllAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (type == 0) {
        } else if (type == 1) {
        } else if (type == 2) {
        }
        SingleDetailItemBean bean = datas.get(position);
        // TODO: 2018/2/27 根据接口返回的字段，判断状态，显示不同的图片资源(本地图片)
        //holder.allEquipState.setImageResource();
        if (bean.getName().contains("2") || bean.getName().contains("4") || bean.getName().contains("6")) {
            holder.allEquipState.setImageResource(R.drawable.equipment_normal);
        }
        holder.allTvName.setText(bean.getName());

        holder.itemBaseContainer.setOnClickListener(v -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onClickListener(position);
            }
        });
        //
        ImageView rightHandleIcon = holder.allRightHandleIcon;
        rightHandleIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onOnHandleBtnClick(position, rightHandleIcon);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //最外层容器
        LinearLayout itemBaseContainer;
        //All页面
        private RelativeLayout itemSingleAllContainer;
        private TextView allTvName;
        private ImageView allEquipState;
        private ImageView allRightHandleIcon;


        public ViewHolder(View itemView) {
            super(itemView);
            itemBaseContainer = itemView.findViewById(R.id.itemBaseContainer);

            /**-----------------------------All-------------------------------------*/
            itemSingleAllContainer = itemView.findViewById(R.id.itemSingleAllContainer);
            allTvName = itemView.findViewById(R.id.allTvName);
            allEquipState = itemView.findViewById(R.id.allEquipState);
            allRightHandleIcon = itemView.findViewById(R.id.allRightHandleIcon);
            /**---------------------------------------------------------------------*/

        }
    }

    /**
     * 回调
     */
    private onItemClickListener mOnItemClickListener;

    public interface onItemClickListener {
        void onClickListener(int position);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 右边图片点击回调
     */
    private onRightHandleIconClickListener mListener;

    public interface onRightHandleIconClickListener {
        void onOnHandleBtnClick(int position, View view);
    }

    public void setOnHandleBtnClickListener(onRightHandleIconClickListener l) {
        this.mListener = l;
    }

}
